package com.tencent.compose.sample.mainpage.sectionItem

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.layout.LazyLayoutItemProvider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import org.jetbrains.compose.ui.tooling.preview.Preview

@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
internal fun LazyLayoutDemo() {
    val items = remember { (0..100).toList().map { it.toString() } }

    val itemProvider = remember {
        object : LazyLayoutItemProvider {
            override val itemCount: Int
                get() = items.size

            @Composable
            override fun Item(index: Int, key: Any) {
                Box(
                    modifier = Modifier
                        .width(100.dp)
                        .height(100.dp)
                        .background(color = if (index % 2 == 0) Color.Red else Color.Green)
                ) {
                    Text(text = items[index])
                }
            }
        }
    }

    // 记录水平滚动偏移量
    var scrollOffset by remember { mutableStateOf(0) }

    SubcomposeLayout(modifier = Modifier.size(500.dp)) { constraints ->
        val placeablesCache = mutableListOf<Pair<Placeable, Int>>()

        fun Placeable.mainAxisSize() = this.width
        fun Placeable.crossAxisSize() = this.height

        val childConstraints = Constraints(maxWidth = Constraints.Infinity, maxHeight = constraints.maxHeight)

        var currentItemIndex = 0
        var crossAxisSize = 0
        var mainAxisSize = 0

        // 测量所有子项（移除宽度限制条件，确保100个子项都被测量）
        while (currentItemIndex < itemProvider.itemCount) {
            val itemPlaceable = subcompose(currentItemIndex) {
                itemProvider.Item(currentItemIndex, currentItemIndex)
            }.first().measure(childConstraints)

            placeablesCache.add(itemPlaceable to mainAxisSize)

            mainAxisSize += itemPlaceable.mainAxisSize()
            crossAxisSize = maxOf(crossAxisSize, itemPlaceable.crossAxisSize())

            currentItemIndex++
        }

        val layoutWidth = mainAxisSize
        val layoutHeight = crossAxisSize

        layout(layoutWidth, layoutHeight) {
            // 基于滚动偏移量放置子项
            for ((placeable, position) in placeablesCache) {
                placeable.place(position - scrollOffset, 0)
            }
        }
    }

    Row(
        modifier = Modifier
            .fillMaxWidth()
    ) {
        // 左侧按钮：居左显示
        Box(modifier = Modifier.size(200.dp)) {
            Text(
                text = "点击增加偏移",
                modifier = Modifier
                    .background(Color.White)
                    .clickable { scrollOffset += 100 }
            )
        }

        // 右侧按钮：居右显示
        Box(modifier = Modifier.size(200.dp)) {
            Text(
                text = "点击减少偏移",
                modifier = Modifier
                    .background(Color.White)
                    .clickable { scrollOffset -= 100 }
            )
        }
    }
}